var app = getApp();
var model = require('../../model/bubbleSort.js')
const request = require('../../utils/request.js')
Page({
  data: {
    nav_title: '综合',
    salesHighToLow: false,
    showDropDownOptions: false,
    nav_content: ['综合', '最新上架', '价格最低', '价格最高', '评价最多'],
    /**
     * 本地测试数据
     */
    item_list: [{
        id: 0,
        img: '../../../images/goods1.jpg',
        name: 'good1',
        price: 999999.99,
        comment__transferred_num: '9万',
        comment_num: 90000,
        item_mark: '4.3',
        sale_num: 218398,
        upload_time: 20170901
      },
      {
        id: 1,
        img: '../../../images/goods1.jpg',
        name: 'good2',
        price: 999.99,
        comment__transferred_num: '9999',
        comment_num: 9999,
        item_mark: '4.5',
        sale_num: 21830,
        upload_time: 20180103
      },
      {
        id: 2,
        img: '../../../images/goods1.jpg',
        name: 'good3',
        price: 9.99,
        comment__transferred_num: '999',
        comment_num: 999,
        item_mark: '4.7',
        sale_num: 2183,
        upload_time: 20171203
      },
      {
        id: 3,
        img: '../../../images/goods1.jpg',
        name: 'good4',
        price: 99999.99,
        comment__transferred_num: '99',
        comment_num: 99,
        item_mark: '4.2',
        sale_num: 3183,
        upload_time: 20170503
      },
      {
        id: 4,
        img: '../../../images/goods1.jpg',
        name: 'good5',
        price: 0.9,
        comment__transferred_num: '99万',
        comment_num: 990000,
        item_mark: '5.0',
        sale_num: 2183000,
        upload_time: 20170903
      }
    ],
    /**
     * 本地测试数据
     */
    sub_cate: [{
        id: 0,
        name: "单肩/斜挎包",
        img: '../../../images/goods1.jpg'
      },
      {
        id: 1,
        name: "单肩包",
        img: '../../../images/goods1.jpg'
      },
      {
        id: 2,
        name: "男双肩包",
        img: '../../../images/goods1.jpg'
      },
      {
        id: 3,
        name: "箱包",
        img: '../../../images/goods1.jpg'
      },
      {
        id: 4,
        name: "男手提公文包",
        img: '../../../images/goods1.jpg'
      },
      {
        id: 5,
        name: "女单肩/公文包",
        img: '../../../images/goods1.jpg'
      },
    ]
  },

  onLoad: function(options) {
    var that = this;
    var categoryId = -1;
    var keyword = '';
    var storeId = options.storeId;
    console.log(storeId);
    if (options.categoryId) {
      categoryId = options.categoryId
      that.categorySearch(storeId, categoryId);
    } else if (options.keyword) {
      keyword = options.keyword;
      that.keywordSearch(storeId, keyword);
    }
  },
  /**关键词搜索 */
  keywordSearch: function(storeId, keyword) {
    var url = request.URL + '/search/detail'
    request.request({
      url: url,
      data: {
        storeId: storeId,
        keyWord: keyword
      },
      success: function(res) {
        console.log(res);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**分类搜索 */
  categorySearch: function(storeId, categoryId) {
    var url = request.URL + '/search/category';
    var categoryId =
      request.request({
        url: url,
        data: {
          storeId: storeId,
          category: categoryId
        },
        success: function(res) {
          console.log(res);
        },
        fail: function() {
          request.showFailInfo()
        }
      })
  },
  //打开下拉条
  click_nav: function(e) {
    var that = this;
    if (that.data.showDropDownOptions) {
      that.setData({
        showDropDownOptions: false
      })
    } else {
      that.setData({
        showDropDownOptions: true
      })
    }

    console.log(that.data.nav_content);
  },
  //选择商品列表排序方法
  chooseOption: function(e) {
    var that = this;
    var array = that.data.item_list;
    var title = e.currentTarget.id;
    switch (title) {
      case "最新上架":
        var arr = model.bubbleSort_highToLow(array, "upload_time");
        that.setData({
          item_list: arr
        })
        break;
      case "价格最低":
        var arr = model.bubbleSort_lowToHigh(array, "price");
        that.setData({
          item_list: arr
        })
        break;
      case "价格最高":
        var arr = model.bubbleSort_highToLow(array, "price");
        that.setData({
          item_list: arr
        })
        break;
      case "评价最多":
        var arr = model.bubbleSort_highToLow(array, "comment_num");
        that.setData({
          item_list: arr
        })
        break;
      default:
        var arr = model.bubbleSort_lowToHigh(array, "id");
        that.setData({
          item_list: arr
        })
        break;
    }
    var that = this;
    that.setData({
      nav_title: title,
      showDropDownOptions: false,
      salesHighToLow: false
    })
  },
  //销量从高到低
  salesHighToLow: function(e) {
    var that = this;
    var array = that.data.item_list;
    if (that.data.salesHighToLow) {
      var arr = model.bubbleSort_lowToHigh(array, "id");
      that.setData({
        item_list: arr
      })
      that.setData({
        salesHighToLow: false,
      })
    } else {
      that.setData({
        salesHighToLow: true,
        nav_title: '综合'
      })
      var arr = model.bubbleSort_highToLow(array, "sale_num");
      that.setData({
        item_list: arr
      })
    }
  },
  //搜索
  search: function(e) {
    console.log(e.currentTarget.dataset.title)
    wx.navigateTo({
      url: '../search/search',
      success: function(res) {
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },

  //隐藏下拉条
  hideDropDownOptions: function(e) {
    var that = this;
    that.setData({
      showDropDownOptions: false
    })
  }
})